//
//  AnswerDetailController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/19.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "AnswerDetailController.h"
#import "HomeFocusCell.h"
#import "AnswerDetailTwoController.h"
#import "FriendsListController.h"


@interface AnswerDetailController ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataSoure;
/**底部视图*/
@property (nonatomic,strong)AnswerDetailBottomView*bottomView;
@end

@implementation AnswerDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"答题详情";
    [self addNavigationItemWithImageNames:@[@"sangedian"] isLeft:NO target:self action:@selector(rightAction) tags:nil];
    [self.view addSubview:self.listView];
    [self.view addSubview:self.bottomView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-50);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
}
-(void)rightAction{
    PresentationObject * object = [PresentationObject sharedPresentationObject];
    object.subViewFrame = CGRectZero;
    [object showPresentingViewController:self PresentedViewController:[ShareViewController new]];

}


#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSoure.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * array = self.dataSoure[section];
//    if (section == 0) {
//        return 1;
//    }
    return array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AnswerDetailCell * oneCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCell class])];
    
 
    HomeFocusMorenImageCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeFocusMorenImageCell class])];
    cell.topView.deleButton.hidden = YES;
    cell.topView.dynamicData.text = @"美食达人";
    
    AnswerDetailModel * model = self.dataSoure[indexPath.section][indexPath.row];
    
    if (indexPath.section == 0) {
        oneCell.model = model;
        return oneCell;
    }
    cell.answermodel = model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section > 0) {
        AnswerDetailTwoController * vc = [AnswerDetailTwoController new];
        //对点击的cell进行判断，是自己的回答内容还是其他人的，然后对type进行赋值
//        vc.type = AnswerDetailMe;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark ---懒加载
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[HomeFocusMorenImageCell class] forCellReuseIdentifier:NSStringFromClass([HomeFocusMorenImageCell class])];
        [_listView registerClass:[AnswerDetailCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCell class])];

    }
    return _listView;
}
-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        _dataSoure =[[NSMutableArray alloc] init];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"AnswerDetail" ofType:@"plist"];
               //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        
        for (NSArray * oneArray in array) {
            NSMutableArray * oneModelArray = [NSMutableArray array];
            for (NSDictionary * dic in oneArray) {
                AnswerDetailModel *model = [AnswerDetailModel new];
                [model setValuesForKeysWithDictionary:dic];
                
                if (model.imageNumber.integerValue > 0) {
                    NSMutableArray * imageArray = [NSMutableArray array];
                    for (NSInteger i = 0 ; i < model.imageNumber.integerValue; i++) {
                        [imageArray addObject:[Utilis getRandomDynamicImage]];
                    }
                    model.imageArray = imageArray;
                }
                
                [oneModelArray addObject:model];

            }
            [_dataSoure addObject:oneModelArray];
        }
            
    }
    return _dataSoure;
}
-(AnswerDetailBottomView*)bottomView{
    if(!_bottomView){
        _bottomView=[[AnswerDetailBottomView alloc] initWithFrame:CGRectZero];
    }
    return _bottomView;
}
@end
//
@interface AnswerDetailCell ()
/**<#属性#>*/
@property (nonatomic,strong)UserIconAndNameView*topView;
/**<#属性#>*/
@property (nonatomic,strong)UILabel*contenLabel;

@end

@implementation AnswerDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setModel:(AnswerDetailModel *)model{
    _model = model;
    self.topView.iconImage.image = IMAGE_NAMED(model.userIcon);
    self.topView.userName.text =model.name;
    self.contenLabel.text = model.title;
}
-(void)createUI{
    
    UILabel * leftLabel = UILabel.new;
    leftLabel.text = @"12个回答  345人收藏";
    leftLabel.textColor = UIColorFromRGB(0x959E97);
    leftLabel.font = SYSTEMFONT(12);
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"邀请回答" forState:UIControlStateNormal];
    [rightBtn setImage:IMAGE_NAMED(@"邀请回答") forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x454F44) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SYSTEMFONT(14);
    ViewBorderRadius(rightBtn, 0, 1, UIColorFromRGB(0xE9EEE7));
    [rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, -4)];
    kWeakSelf(weakSelf);
    [rightBtn addActionHandler:^{
        [[weakSelf getCurrentViewController].navigationController pushViewController:[FriendsListController new] animated:YES];
    }];
    
    [self addSubview:self.topView];
    [self addSubview:self.contenLabel];
    [self addSubview:leftLabel];
    [self addSubview:rightBtn];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    
    [self.contenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.right.mas_equalTo(-16);
    }];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(self.contenLabel.mas_bottom).offset(25);
        make.bottom.mas_equalTo(-25);
    }];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(leftLabel);
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(104);
        make.height.mas_equalTo(36);
    }];

}
-(UserIconAndNameView*)topView{
    if(!_topView){
        _topView=[[UserIconAndNameView alloc] initWithFrame:CGRectZero];
        [_topView.deleButton setBackgroundImage:IMAGE_NAMED(@"guanzhu_bg") forState:UIControlStateNormal];
        [_topView.deleButton setTitle:@"关注" forState:UIControlStateNormal];
        [_topView.deleButton setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        _topView.deleButton.titleLabel.font = SYSTEMFONT(14);
        _topView.dynamicData.text = @"美食达人";
        [_topView.deleButton addActionHandler:^{
            
        }];

    }
    return _topView;
}
-(UILabel*)contenLabel{
    if(!_contenLabel){
        _contenLabel=[UILabel new];
        _contenLabel.textColor = UIColorFromRGB(0x273323);
        _contenLabel.font = BOLDSYSTEMFONT(18);
        _contenLabel.numberOfLines = 0;
        _contenLabel.text = @"西红柿炒蛋先放西红柿还是先放鸡蛋？";
    }
    return _contenLabel;
}

@end
